#pragma once
#include <iostream>
#include <unordered_map>
#include <string>
#include<vector>
#include<fstream>
//#include"Log.hpp"
#include"ThreadPool.hpp"

using namespace std;

const string unknown = "Unknown";
const string mydict = "./resource/dict.txt";


const string sep=" ";

class Translate
{
    public:
    Translate(string dict_path = mydict)
        : _dict_path(dict_path)
    {
        LoadDict();
        Parse();
    }
    void LoadDict()
    {
        ifstream ifs(_dict_path);

        string line;
        while (getline(ifs, line))
        {
            _lines.push_back(line);
        }
        ifs.close();
        lg.LogMessage(Debug,"Locad dict txt success ,path:%s\n",_dict_path.c_str());
    }

    void PrintDebug()//测试是否将中./resource/dict.txt的内容读入
    {
        //将txt文件的内容读到_lines中
        // for(auto& line:_lines)
        // {
        //     cout << line << endl;
        // }

        //遍历字典_dict
        cout<<endl;
        for(auto& elem:_dict)
        {
            cout << elem.first << ":" << elem.second << endl;
        }
    }

    void Parse()
    {
        for(auto& line:_lines)
        {
            auto pos = line.find(sep);
            if(pos==string::npos) 
                continue;
            else
            {
                string word=line.substr(0,pos);//取出单词
                string trans=line.substr(pos+1);////取出翻译

                //_dict.insert(make_pair(word,trans));//将单词和翻译存入字典
                _dict[word]=trans;//将单词和翻译存入字典
            }
        }
        lg.LogMessage(Debug,"Parse dict txt success ,path:%s\n",_dict_path.c_str());
    }

    ~Translate() {}

    string Excute(const string &word)
    {
        auto iter = _dict.find(word);
        if (iter != _dict.end())
            return _dict[word];
        else
            return unknown;
    }

private:
    string _dict_path;
    vector<string> _lines;
    unordered_map<string, string> _dict;
};
